package com.wangyi.fightimg.widget.sharepopup;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wangyi.fightimg.R;
import com.wangyi.fightimg.moduls.main.MainFragmentAdapter;

import java.util.List;

/**
 * Created by tiger on 2017/7/28.
 */

public class SharePopupAdapter extends RecyclerView.Adapter<SharePopupAdapter.MyHolder> {

    private OnItemClickListener onItemClickListener;// 列表项点击事件监听
    private List<SharePlatform> sharePlatforms;// 列表项实体类集合
    private Context context;

    public SharePopupAdapter(Context context, List<SharePlatform> sharePlatforms, OnItemClickListener onItemClickListener) {
        this.context = context;
        this.sharePlatforms = sharePlatforms;
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 列表项点击事件接口
     */
    public interface OnItemClickListener {
        void setOnItemClick(int log);
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_share, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        final SharePlatform sharePlatform = sharePlatforms.get(position);
        holder.ivLog.setImageResource(sharePlatform.getPlatformLog());
        holder.tvName.setText(sharePlatform.getPlatformName());
        // 列表项点击事件
        holder.llItemRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.setOnItemClick(sharePlatform.getPlatformLog());
            }
        });
    }

    @Override
    public int getItemCount() {
        return sharePlatforms.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        LinearLayout llItemRoot;// 根布局
        ImageView ivLog;// 平台Log
        TextView tvName;// 平台名称

        public MyHolder(View itemView) {
            super(itemView);
            llItemRoot = (LinearLayout) itemView.findViewById(R.id.ll_item_root);
            ivLog = (ImageView) itemView.findViewById(R.id.iv_log);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
        }
    }
}
